package puppet.student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void studentSystemMain() {
        String option = "";
        Scanner scanner = new Scanner(System.in);
        ArrayList<Student> studentArrayList = new ArrayList<>();

        chose: while(true){
            menu();
            System.out.println("your option:");
            option = scanner.next();

            switch(option){
                case "1" : addStudent(studentArrayList);
                    break;
                case "2" : delStudent(studentArrayList);
                    break;
                case "3" : updateStudent(studentArrayList);
                    break;
                case "4" : viewStudent(studentArrayList);
                    break;
                case "0" : System.exit(0);
                    break chose;
                default:
                    System.out.println("don't have this option,please chose again");
                    break;
            }
        }
    }

    //菜单
    public static void menu(){
        System.out.println("------------------Welcome to StudentSystem-------------------");
        System.out.println("1.addStudent");
        System.out.println("2.delStudent");
        System.out.println("3.updateStudent");
        System.out.println("4.viewStudent");
        System.out.println("0.exit");
    }

    //添加学生信息
    public static void addStudent(ArrayList<Student> studentArrayList){
        Student student = new Student();
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("请输入学生id：");
            String strId = scanner.next();
            boolean flag = isRepeat(studentArrayList,strId);
            if(flag){
                System.out.println("id已存在，请重新输入");
            }
            else {
                student.setId(strId);
                break;
            }
        }

        System.out.println("请输入学生name：");
        String strName = scanner.next();
        student.setName(strName);

        System.out.println("请输入学生age：");
        int strAag = scanner.nextInt();
        student.setAge(strAag);

        System.out.println("请输入学生address：");
        String strAddress = scanner.next();
        student.setAddress(strAddress);

        //放入集合
        studentArrayList.add(student);
    }

    //删除学生信息
    public static void delStudent(ArrayList<Student> studentArrayList){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生id：");
        String strId = scanner.next();
        int index = getIndex(studentArrayList,strId);
        if(index >= 0){
            studentArrayList.remove(index);
            System.out.println("删除成功");
        }else {
            System.out.println("id不存在");
        }
    }

    //修改学生信息
    public static void updateStudent(ArrayList<Student> studentArrayList){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生id：");
        String strId = scanner.next();
        int index = getIndex(studentArrayList,strId);
        if(index == -1){
            System.out.println("id不存在");
            return;
        }

        Student student = studentArrayList.get(index);
        System.out.println("请输入学生name：");
        String newName = scanner.next();
        student.setName(newName);

        System.out.println("请输入学生age：");
        int newAag = scanner.nextInt();
        student.setAge(newAag);

        System.out.println("请输入学生address：");
        String newAddress = scanner.next();
        student.setAddress(newAddress);
    }

    //查看所有学生信息
    public static void viewStudent(ArrayList<Student> studentArrayList){
        if(studentArrayList.size() == 0){
            System.out.println("没有录入的学生");
            return;
        }
        System.out.println("id \t name \t age \t address");
        for (int i = 0; i < studentArrayList.size(); i++) {
            Student student = studentArrayList.get(i);
            System.out.println(student.getId() + "\t" +
                    student.getName() + "\t" +
                    student.getAge() + "\t" +
                    student.getAddress());
        }
    }

    //判断id是否存在
    public static boolean isRepeat(ArrayList<Student> studentArrayList,String id){
        for (int i = 0; i < studentArrayList.size(); i++) {
            if(id.equals(studentArrayList.get(i).getId())){
                return true;
            }
        }
        return false;
    }

    //获取存在id的索引
    public static int getIndex(ArrayList<Student> studentArrayList,String id){
        for (int i = 0; i < studentArrayList.size(); i++) {
            if(id.equals(studentArrayList.get(i).getId())){
                return i;
            }
        }
        return -1;
    }
}
